//
//  SpecialBodyView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/18.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class SpecialBodyView: UIButton {
    
    // 日期
    private lazy var timeView = UILabel()
    // 票价
    private lazy var priceView = UILabel()
    // 景点图片
    private lazy var iconView = UIImageView()
    // 箭头图片
    private lazy var arrowView = UIImageView()
    // 到达城市
    private lazy var stopCityView = UILabel()
    // 出发城市
    private lazy var startCityView = UILabel()
    
    var time:String? {
        didSet {
            timeView.text = AirLineUtil.shareInstance.getConvertDate(time: time!)
        }
    }
    
    var price:Int? {
        didSet {
            priceView.text = "¥\(price!)起"
        }
    }
    
    var icon:String? {
        didSet {
            iconView.image = UIImage(named: icon!)
        }
    }
    
    var startCity:String? {
        didSet {
            startCityView.text = startCity!
        }
    }
    
    var stopCity:String? {
        didSet {
            stopCityView.text = stopCity!
        }
    }
    
    override func layoutSubviews() {
        init_iconView()
        init_startCityView()
        init_arrowView()
        init_stopCityView()
        init_timeView()
        init_priceView()
    }
    
    // MARK: 日期
    private func init_timeView() -> Void {
        timeView.textColor = UIColor.gray
        timeView.font = UIFont.systemFont(ofSize: 12)
        timeView.frame = CGRect(x: MARGIN10, y: startCityView.frame.maxY + MARGIN5, width: frame.width * 0.4, height: MARGIN)
        addSubview(timeView)
    }
    
    // MARK: 票价
    private func init_priceView() -> Void {
        priceView.textColor = COLOR_RED
        priceView.font = UIFont.systemFont(ofSize: 12)
        guard let textWidth = priceView.text?.boundingRect(w: MARGIN * 3, h: MARGIN, fontSize: 12).width else {
            return
        }
        priceView.frame = CGRect(x:frame.width - textWidth - MARGIN10, y: startCityView.frame.maxY + MARGIN5, width: MARGIN * 3, height: MARGIN)
        addSubview(priceView)
    }
    
    // MARK: 景点图片
    private func init_iconView() -> Void {
        iconView.contentMode = .scaleToFill
        iconView.layer.cornerRadius = 5
        iconView.layer.masksToBounds = true
        iconView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height * 0.6)
        addSubview(iconView)
    }
    
    // MARK: 箭头图片
    private func init_arrowView() -> Void {
        arrowView.image = UIImage(named: "checkinarrow")
        arrowView.contentMode = .scaleAspectFit
        guard let textWidth = startCityView.text?.boundingRect(w: MARGIN * 4, h: MARGIN, fontSize: 12).width else {
            return
        }
        arrowView.frame = CGRect(x: MARGIN10 * 2 + textWidth, y: iconView.frame.maxY + MARGIN10, width: MARGIN, height: MARGIN)
        addSubview(arrowView)
    }
    
    // MARK: 到达城市
    private func init_stopCityView() -> Void {
        stopCityView.textColor = COLOR_NICE
        stopCityView.font = UIFont.systemFont(ofSize: 13)
        guard let width = startCityView.text?.boundingRect(w: MARGIN * 4, h: MARGIN, fontSize: 12).width else {
            return
        }
        let textWidth = MARGIN10 * 3 + width + arrowView.bounds.width
        stopCityView.frame = CGRect(x: textWidth, y: iconView.frame.maxY + MARGIN10, width: MARGIN * 4, height: MARGIN)
        addSubview(stopCityView)
    }
    
    // MARK: 出发城市
    private func init_startCityView() -> Void {
        startCityView.textColor = COLOR_NICE
        startCityView.font = UIFont.systemFont(ofSize: 13)
        startCityView.frame = CGRect(x: MARGIN10, y: iconView.frame.maxY + MARGIN10, width: MARGIN * 4, height: MARGIN)
        addSubview(startCityView)
    }
}

